﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web.Security;
using QuestionsAnswers.UI.Models.QuestionsAnswers;

namespace QuestionsAnswers.UI.Models
{
    public class QuestionsAnswersContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<Answer> Answers { get; set; }
        public DbSet<Rating> Ratings { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Invitation> Invitations { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<AssignedToUser> AssignedToUsers { get; set; }
        public DbSet<Tag> Tags { get; set; }



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Entity<Question>().HasMany(q => q.Answers).WithRequired(q => q.Question).HasForeignKey(a => a.QuestionId).WillCascadeOnDelete(true);
            modelBuilder.Entity<Question>().HasMany(q => q.AssignedToUsers).WithRequired(q => q.Question).HasForeignKey(a => a.QuestionId).WillCascadeOnDelete(true);
            modelBuilder.Entity<Course>().HasMany(c => c.Invitations).WithRequired(c => c.Course).HasForeignKey(a => a.CourseId).WillCascadeOnDelete(true);
            modelBuilder.Entity<Course>().HasMany(c => c.AssignedToUsers).WithRequired(c => c.Course).HasForeignKey(a => a.CourseId).WillCascadeOnDelete(true);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Entity<Answer>().HasMany(a => a.Comments).WithRequired(a => a.Answer).HasForeignKey(c => c.AnswerId).WillCascadeOnDelete(true);
        }


    }

    //             public static void Main(String [] args)
    //         {
    //             Database.SetInitializer<SurveyContext>(new SurveyCometInit());
    //         }


    public class QuestionsAnswersInitLocal : DropCreateDatabaseAlways<QuestionsAnswersContext>
    {
        protected override void Seed(QuestionsAnswersContext context)
        {
            try
            {
                Membership.CreateUser("noCourse", "qqqqqq", "foo4@example.com");
                Membership.CreateUser("bilit", "qqqqqq", "foo1@example.com");
                Membership.CreateUser("blitz", "qqqqqq", "fo2@example.com");
                Membership.CreateUser("bill", "qqqqqq", "foo3@example.com");
            }
            catch (Exception)
            {
            }



            // context.Configuration.LazyLoadingEnabled = true;


            User user1 = new User { UserName = "bilit" };
            User user2 = new User { UserName = "blitz" };
            User user3 = new User { UserName = "bill" };
            User user4 = new User { UserName = "noCourse" };

            var users = new List<User> { user3, user1 };

            Course course1 = new Course
            {
                Name = "course1",
                Creator = user1,
                Owners = users
            };

            Course course2 = new Course
            {
                Name = "course2",
                Creator = user2,
                Participants = users
            };

            Course course3 = new Course
            {
                Name = "course3",
                Creator = user3,
                Participants = users,
                Owners = users

            };

            Invitation invite = new Invitation
            {
                Course = course1,
                InvitedEmail = "foo3@example.com",
                Inviter = user1,
                MemberType = "Participant"
            };


            var courses = new List<Course> { course3 };
            Question question1 = new Question
            {
                Text = "What color is George Washington's white horse?",
                User = user1,
                Courses = new List<Course>()
            };
            Question question2 = new Question
            {
                Text = "What flavor is chocolate ice cream?",
                User = user2,
                Courses = new List<Course>()
            };

            Question question3 = new Question
            {
                Text = "What flavor is vanilla ice cream?",
                User = user3,
                Courses = courses,
            };
            Question question4 = new Question
            {
                Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tellus nunc, consequat blandit ornare quis, interdum id enim. Curabitur sit amet gravida sapien. Nullam elementum felis ut lectus sollicitudin euismod. Aliquam quis gravida purus. Aliquam tempus pretium mauris in ornare. Aenean sed luctus diam. Vivamus tincidunt, nulla at elementum laoreet, est turpis scelerisque tellus, tempor condimentum velit nisl ac arcu. Vivamus vitae orci urna, at pellentesque dolor. Aenean semper massa sed mauris blandit convallis. Maecenas id turpis eu augue semper pharetra. Quisque consectetur odio ut velit venenatis tristique semper magna semper. Aliquam varius sapien et turpis dictum ut vulputate diam mollis. Etiam ultricies posuere fermentum. Nulla sit amet est eu lectus adipiscing dapibus eget et velit. Donec et porttitor enim.",
                User = user3,
                Courses = new List<Course>()
            };
            Question question5 = new Question
            {
                Text = "Mauris sit amet augue leo. Vivamus vulputate tincidunt ante, quis elementum leo luctus sit amet. In egestas hendrerit venenatis. Suspendisse posuere magna a sapien vehicula porta. Donec imperdiet nibh aliquet ante faucibus in hendrerit ligula sodales. Fusce et neque et orci ultricies euismod ac sed quam. Suspendisse in diam enim.",
                User = user3,
                Courses = new List<Course>()
            };
            Question question6 = new Question
            {
                Text = "Nam ut dui sed turpis imperdiet hendrerit quis non leo. Donec luctus laoreet felis non semper. Nulla aliquet congue leo, non pulvinar urna posuere vel. Maecenas vitae vehicula nisi. Donec nibh tellus, ultrices ac blandit ac, lacinia porta risus. Sed convallis ultrices enim sed mollis. Nunc metus libero, adipiscing et sagittis at, commodo at orci. Quisque fringilla ultricies magna ac feugiat. Quisque vel lacus lorem. Praesent auctor justo sit amet augue congue semper non id ante.",
                User = user3,
                Courses = new List<Course>()
            };

            Question question7 = new Question
            {
                Text = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth",
                User = user3,
                Courses = new List<Course>()
            };

            question5.DateCreated = DateTime.Today.AddDays(-1);
            question6.DateCreated = DateTime.Today.AddDays(1);

            List<Answer> answers1 = new List<Answer>
                                       {
                                           new Answer()
                                               {
                                                   User = user1,
                                                   Question = question1,
                                                   Text = "White"
                                               }
                                       };

            List<Answer> answers2 = new List<Answer>
                                        {
                                            new Answer()
                                                {
                                                    User = user2,
                                                    Question = question2,
                                                    Text = "Chocolate"
                                                },
                                            new Answer()
                                                {
                                                    User = user3,
                                                    Question = question2,
                                                    Text = "Vanilla"
                                                }
                                        };

            List<Answer> answers3 = new List<Answer>
                                        {
                                            new Answer()
                                                {
                                                    User = user3,
                                                    Question = question3,
                                                    Text = "Vanilla"
                                                },
                                            new Answer()
                                                {
                                                    User = user2,
                                                    Question = question3,
                                                    Text = "Chocolate"
                                                }
                                        };

            AssignedToUser assigned = new AssignedToUser
            {
                Course = course1,
                Question = question3,
                User = user3
            };
            AssignedToUser assigned1 = new AssignedToUser
            {
                Course = course3,
                Question = question3,
                User = user1
            };

            context.Questions.Add(question1);
            context.Questions.Add(question2);
            context.Questions.Add(question3);
            context.Questions.Add(question4);
            context.Questions.Add(question5);
            context.Questions.Add(question7);

            context.Courses.Add(course1);
            context.Courses.Add(course2);
            context.Courses.Add(course3);
            context.Invitations.Add(invite);

            context.AssignedToUsers.Add(assigned);
            context.AssignedToUsers.Add(assigned1);
            answers1.ForEach(answer => context.Answers.Add(answer));
            answers2.ForEach(answer => context.Answers.Add(answer));
            answers3.ForEach(answer => context.Answers.Add(answer));
            context.Users.Add(user4);

            context.SaveChanges();



            context.SaveChanges();

        }
    }


    public class QuestionsAnswersInit : System.Data.Entity.IDatabaseInitializer<QuestionsAnswersContext>
    {
        public void InitializeDatabase(QuestionsAnswersContext context)
        {
            if (Membership.GetUser("bill") == null) // first time
            {
                Membership.CreateUser("noCourse", "qqqqqq", "foo4@example.com");
                Membership.CreateUser("bilit", "qqqqqq", "foo1@example.com");
                Membership.CreateUser("blitz", "qqqqqq", "fo2@example.com");
                Membership.CreateUser("bill", "qqqqqq", "foo3@example.com");




                // context.Configuration.LazyLoadingEnabled = true;


                User user1 = new User { UserName = "bilit" };
                User user2 = new User { UserName = "blitz" };
                User user3 = new User { UserName = "bill" };
                User user4 = new User { UserName = "noCourse" };

                var users = new List<User> { user3, user1 };

                Course course1 = new Course
                                     {
                                         Name = "course1",
                                         Creator = user1,
                                         Owners = users
                                     };

                Course course2 = new Course
                                     {
                                         Name = "course2",
                                         Creator = user2,
                                         Participants = users
                                     };

                Course course3 = new Course
                                     {
                                         Name = "course3",
                                         Creator = user3,
                                         Participants = users,
                                         Owners = users

                                     };

                Invitation invite = new Invitation
                                        {
                                            Course = course1,
                                            InvitedEmail = "foo3@example.com",
                                            Inviter = user1,
                                            MemberType = "Participant"
                                        };


                var courses = new List<Course> { course3 };
                Question question1 = new Question
                                         {
                                             Text = "What color is George Washington's white horse?",
                                             User = user1,
                                             Courses = new List<Course>()
                                         };
                Question question2 = new Question
                                         {
                                             Text = "What flavor is chocolate ice cream?",
                                             User = user2,
                                             Courses = new List<Course>()
                                         };

                Question question3 = new Question
                                         {
                                             Text = "What flavor is vanilla ice cream?",
                                             User = user3,
                                             Courses = courses,
                                         };
                Question question4 = new Question
                                         {
                                             Text = "question",
                                             User = user3,
                                             Courses = new List<Course>()
                                         };
                Question question5 = new Question
                                         {
                                             Text = "question",
                                             User = user3,
                                             Courses = new List<Course>()
                                         };
                Question question6 = new Question
                                         {
                                             Text = "question",
                                             User = user3,
                                             Courses = new List<Course>()
                                         };

                List<Answer> answers1 = new List<Answer>
                                            {
                                                new Answer()
                                                    {
                                                        User = user1,
                                                        Question = question1,
                                                        Text = "White"
                                                    }
                                            };

                List<Answer> answers2 = new List<Answer>
                                            {
                                                new Answer()
                                                    {
                                                        User = user2,
                                                        Question = question2,
                                                        Text = "Chocolate"
                                                    },
                                                new Answer()
                                                    {
                                                        User = user3,
                                                        Question = question2,
                                                        Text = "Vanilla"
                                                    }
                                            };

                List<Answer> answers3 = new List<Answer>
                                            {
                                                new Answer()
                                                    {
                                                        User = user3,
                                                        Question = question3,
                                                        Text = "Vanilla"
                                                    },
                                                new Answer()
                                                    {
                                                        User = user2,
                                                        Question = question3,
                                                        Text = "Chocolate"
                                                    }
                                            };

                AssignedToUser assigned = new AssignedToUser
                                              {
                                                  Course = course1,
                                                  Question = question3,
                                                  User = user3
                                              };
                AssignedToUser assigned1 = new AssignedToUser
                                               {
                                                   Course = course3,
                                                   Question = question3,
                                                   User = user1
                                               };

                context.Questions.Add(question1);
                context.Questions.Add(question2);
                context.Questions.Add(question3);
                context.Questions.Add(question4);
                context.Questions.Add(question5);

                context.Courses.Add(course1);
                context.Courses.Add(course2);
                context.Courses.Add(course3);
                context.Invitations.Add(invite);

                context.AssignedToUsers.Add(assigned);
                context.AssignedToUsers.Add(assigned1);
                answers1.ForEach(answer => context.Answers.Add(answer));
                answers2.ForEach(answer => context.Answers.Add(answer));
                answers3.ForEach(answer => context.Answers.Add(answer));
                context.Users.Add(user4);

                context.SaveChanges();



                context.SaveChanges();
            }

        }

    }
}